/**
 * Created by dale on 2017/9/13.
 */

var flag=true;
var targetObj; //生成的目标target容器
var $clickFlag; //扫码区分

$('#code').textbox({
    onChange:function (newValue,oldValue) {  //二维码确认事件
        $.ajax({
            url:baseUrl+'ims/moblie/getBPInfo',
            dataType:'json',
            type:'post',
            data:{code:newValue},
            success:function (data) {
                console.log(data);
                if(flag){
                    appendSuccess(data);
                }else {
                    targetObj.remove();
                    appendSuccess(data);

                }

            },
            error:function (data) {
                if(flag){
                    appendError();
                }else {
                    targetObj.remove();
                    appendError();
                }
            }
        })
    }
});





/**
 * 扫码回调
 * @type {{setResult: JSObjectHandle.setResult}}
 * created by dale 2017/9/15
 */
var JSObjectHandle={
    setResult:function (code) {
        setTimeout(function () {
            if($clickFlag==1){
                $('#code').textbox('setValue',code);
                $('#code').textbox({editable:false});
            }else {
                $('#sBoxCode').textbox('setValue',code);
                $('#sBoxCode').textbox({editable:false});
            }
        },0);
    }
}

$('#ff').on('click',('.textbox-button'),function () {  //textBox按钮点击事件

    var id=$(this).parent().prev().attr('id');
    if(id=='code'){
        console.log('aaa');
        $clickFlag=1;
    }else {
        console.log('bb');
        $clickFlag=2;
    }
    // window.webkit.messageHandlers.openScanf.postMessage(null);
    window.top.openScan();
});



/**
 * 没有找到表单样式
 * created by dale 2017/9/15
 */
function appendError() {
    var content='';
    content += '<div style="margin-bottom:10px">'
                 +'<select id="iTypeId" class="easyui-combobox" label="分类:" name="iTypeId"  style="width:100%" data-options="editable:false,labelPosition:\'top\'">';
    content +='</select></div>';

    content += '<div style="margin-bottom:10px">'
        +'<select id="iProdId" class="easyui-combobox" label="商品:" name="iProdId"  style="width:100%" data-options="editable:false,labelPosition:\'top\'">';
    content +='</select></div>';

    content += '<div style="margin-bottom:10px">'
        +'<input id="sBoxCode" class="easyui-textbox" label="箱码:"  name="sBoxCode" data-options="buttonText:\'扫描\',buttonIcon:\'btn-scan\',prompt:\'扫描货码\',labelPosition:\'top\'" style="width:100%">'
        +'</div>';

    content +='<div style="margin-bottom:10px">'

        +'<a id="saveOut" class="easyui-linkbutton" data-options="iconCls:\'btn-save\'" style="padding:5px 0px;width:100px;float: right;margin-right: 5px;">'
        +'<span style="font-size:14px;">提交</span>'
        +'</a>'
        +'</div>';




    targetObj = $(content).appendTo("#ff");
    $.parser.parse(targetObj);

    $("#iTypeId").combobox({  //商品类型层
        url:baseUrl+'ims/moblie/getTypeOther/false',
        valueField:'id',
        textField:'text',
        onSelect:function (rec) {
            var id = rec['id'];
            $('#iProdId').combobox({   //商品层
                url:baseUrl+'ims/moblie/getProds?id='+id,
                valueField:'iProdId',
                textField:'sProdName',
            });
        },
        onLoadSuccess: function () {
            var val = $(this).combobox("getData");
            $(this).combobox("setValue",val[0]['id']);
        }
    });


    flag=false;

}
/**
 * 成功搜索的添加表单样式
 * @param $data
 * created by dale 2017/9/15
 */
function appendSuccess($data) {
    var content='';
        content += '<div style="margin-bottom:10px">'
                    +'<select id="iTypeId" class="easyui-combobox" label="分类:" name="iTypeId"  style="width:100%" data-options="editable:false,labelPosition:\'top\'">';
                            $.each($data['prodType'],function (i,item) {
                                content+='<option value="'+item['iTypeId']+'">'+item.sTypeName+'</option>';
                            });
        content +='</select></div>';

        content += '<div style="margin-bottom:10px">'
                    +'<select id="iProdId" class="easyui-combobox" label="商品:" name="iProdId"  style="width:100%" data-options="editable:false,labelPosition:\'top\'">';
                        $.each($data['prods'],function (i,item) {
                            content+='<option value="'+item['iProdId']+'">'+item.sProdName+'</option>';
                        });
        content +='</select></div>';

        content += '<div style="margin-bottom:10px">'
                        +'<select id="sMeasure" class="easyui-combobox" label="单位:" name="sMeasure"  style="width:100%" data-options="editable:false,labelPosition:\'top\'">';
                                $.each($data['measure'],function (i,item) {
                                    if(item['select']){
                                        content+='<option value="'+item['text']+'" selected>'+item.text+'</option>';
                                    }else {
                                        content+='<option value="'+item['text']+'">'+item.text+'</option>';
                                    }
                                });
        content +='</select></div>';

    if($data['type']==2){
        content += '<div style="margin-bottom:10px">'
            +'<input id="iLeftNum" class="easyui-numberbox" label="库存:"  name="iLeftNum"  data-options="editable:\'false\',labelPosition:\'top\'" style="width:100%">'
            +'</div>';
        // content += '<div style="margin-bottom:10px">'
        //     +'<input id="iOutNum" class="easyui-numberbox" label="出货数目:"  name="iOutNum"  data-options="labelPosition:\'top\'" style="width:100%">'
        //     +'</div>';
    }

    content +='<div style="margin-bottom:10px">'

        +'<a id="saveIn" class="easyui-linkbutton" data-options="iconCls:\'btn-save\'" style="padding:5px 0px;width:100px;float: right;margin-right: 5px;">'
        +'<span style="font-size:14px;">提交</span>'
        +'</a>'
        +'</div>';


    targetObj = $(content).appendTo("#ff");
    $.parser.parse(targetObj);

    if($data['type']==2){
        $('#iLeftNum').numberbox('setValue',$data['iLeftNum']);
        $('#iLeftNum').numberbox({editable:false});
    }


    $('#iProdId').combobox('setValue',$data['iProdId']);
    $('#iTypeId').combobox('setValue',$data['iTypeId']);
    $('#iTypeId').combobox('disable');
    $('#iProdId').combobox('disable');

    $('#sMeasure').combobox('disable');
    flag=false;
}
/**
 * 提交点击事件
 *
 */
$('#ff').on('click','#saveIn',function () {  //提交表单
    $('#sMeasure').combobox('enable');
    $('#iTypeId').combobox('enable');
    $('#iProdId').combobox('enable');
    $.ajax({
        url:baseUrl+'ims/res/saveOutTab',
        data:$('#ff').serialize(),
        type:'post',
        dataType:'json',
        success:function (data) {
            $('#sMeasure').combobox('disable');
            $('#iTypeId').combobox('disable');
            $('#iProdId').combobox('disable');
            window.top.swal({title:'成功',html: data['msg'],type:'success',confirmButtonText:'确定'});
            getLeftNum($('#code').textbox('getValue'));
        },
        error:function (data) {
            $('#sMeasure').combobox('disable');
            $('#iTypeId').combobox('disable');
            $('#iProdId').combobox('disable');
            window.top.sendErrorMsg(data['responseText']);
        }
    })
});

/**
 * 搜索不到提交事件
 *
 */
$('#ff').on('click','#saveOut',function () {  //提交表单
    $.ajax({
        url:baseUrl+'ims/res/saveOutTab',
        data:$('#ff').serialize(),
        type:'post',
        success:function (data) {
            window.top.swal({title:'成功',html: data['msg'],type:'success',confirmButtonText:'确定'});
        },
        error:function (data) {
            window.top.sendErrorMsg(data['responseText']);
        }
    })
});



/**
 * 获取剩余的数目
 * @param newValue
 */
function getLeftNum(newValue) {
    $.ajax({
        url:baseUrl+'ims/moblie/getLeftNum',
        dataType:'json',
        type:'post',
        data:{code:newValue},
        success:function (data) {
            $('#iLeftNum').numberbox('setValue',data['leftNum']);
            $('#iLeftNum').numberbox({editable:false});
        },
        error:function (data) {
            console.log(data);
        }
    })
}


// function getOutNum(schoolId,year) {
//     $.ajax({
//         url:baseUrl+'ims/moblie/getOutNum',
//         data:{schoolId:schoolId,year:year},
//         type:'post',
//         dataType:'json',
//         success:function (data) {
//             var $str = '合计(2017/9/13)&nbsp;: &nbsp; <font color="#ff8c00">15/300</font>';
//             $('#text').empty();
//             $('#text').append($str);
//         },
//         error:function (data) {
//             sendErrorMsg(data['msg']);
//         }
//     });
//
// }







